<?php

use Drupal\distribution\Entity\Distribution;
use Drupal\shusy_team\Entity\ShusyTeam;

/**
 * Implements hook_rules_action_info().
 */
function shusy_team_rules_action_info() {
  return [
    'shusy_team_rules_action_create_team' => [
      'label' => t('生成团队推广分润关系'),
      'parameter' => [
        'account' => [
          'type' => 'user',
          'label' => t('需要生成关系的用户'),
        ],
      ],
      'group' => t('shusy_team'),
    ],
    'shusy_team_rules_action_upgrade_team' => [
      'label' => t('升级部长-转换团队推广分润关系'),
      'parameter' => [
        'account' => [
          'type' => 'user',
          'label' => t('升级部长的用户'),
        ],
      ],
      'group' => t('shusy_team'),
    ],
  ];
}

function shusy_team_rules_action_create_team($account) {
  $uid = $account->uid;
  $teams = shusy_team_load_multiple(FALSE, ['uid' => $uid]);
  if (!empty($teams)) {
    $team = reset($teams);
  }
  else {
    $team = new ShusyTeam;
    $team->uid = $uid;
  }
  if ($team instanceof ShusyTeam) {
    //分享关系
    $pid = $team->pid;
    $parentTeams = shusy_team_load_multiple(FALSE, ['uid' => $pid]);
    if (!empty($parentTeams)) {
      $parentTeam = reset($parentTeams);
      if ($parentTeam instanceof ShusyTeam) {
        //基于上家当前身份设置分佣收益关系
        switch ($parentTeam->level) {
          case SHUSY_TEAM_LEVEL_MARKET_1:
          case SHUSY_TEAM_LEVEL_MARKET_2:
            $team->market_1 = $uid;
            $team->market_2 = $pid;
            $team->company_1 = $parentTeam->company_1;
            $team->company_2 = $parentTeam->company_2;
            break;
          case SHUSY_TEAM_LEVEL_COMPANY_1:
            $team->market_1 = $uid;
            $team->market_2 = $pid;
            $team->company_1 = $pid;
            $team->company_2 = $parentTeam->company_2;
            break;
          case SHUSY_TEAM_LEVEL_COMPANY_2:
            $team->market_1 = $uid;
            $team->market_2 = $pid;
            $team->company_1 = $pid;
            $team->company_2 = $pid;
            break;
        }
      }
    }
    $team->level = SHUSY_TEAM_LEVEL_MARKET_1;
    $team->status = 1;
    $team->save();
  }
}

function shusy_team_rules_action_upgrade_team($account) {
  $uid = $account->uid;
  $teams = shusy_team_load_multiple(FALSE, ['uid' => $uid]);
  if (!empty($teams)) {
    $team = reset($teams);
    if ($team instanceof ShusyTeam) {
      //上家如果已经升级了部长享受分红型股东收益
      $pidTeam = ShusyTeam::getTeam($team->pid);
      if ($pidTeam instanceof ShusyTeam) {
        if ($pidTeam->level == SHUSY_TEAM_LEVEL_COMPANY_1) {
          $pidTeam->level = SHUSY_TEAM_LEVEL_COMPANY_2;
          $pidTeam->save();
        }
      }
      $team->level = SHUSY_TEAM_LEVEL_COMPANY_1;
      //检查下级是否有升级为部长或股东的 需要按分红股东计算
      $query = db_select('shusy_team', 't');
      $query->fields('t', ['id']);
      $query->condition('pid', $team->uid);
      $query->condition('level', [3, 4], 'IN');
      $result = $query->execute()->rowCount();
      if ($result > 0) {
        $team->level = SHUSY_TEAM_LEVEL_COMPANY_2;
      }
      $team->status = 1;
      $team->save();
    }
  }
}
